home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / lisp / mpuz.el < prev    next >
Lisp/Scheme  |  1993-07-23  |  15KB  |  449 lines

  1. ;;; mpuz.el --- multiplication puzzle for GNU Emacs
  2.  
  3. ;;; Copyright (C) 1990 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Philippe Schnoebelen <phs@lifia.imag.fr>
  6. ;; Keywords: games
  7.  
  8. ;; This file is part of GNU Emacs.
  9.  
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  12. ;; accepts responsibility to anyone for the consequences of using it
  13. ;; or for whether it serves any particular purpose or works at all,
  14. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  15. ;; License for full details.
  16.  
  17. ;; Everyone is granted permission to copy, modify and redistribute
  18. ;; GNU Emacs, but only under the conditions described in the
  19. ;; GNU Emacs General Public License.   A copy of this license is
  20. ;; supposed to have been given to you along with GNU Emacs so you
  21. ;; can know your rights and responsibilities.  It should be in a
  22. ;; file named COPYING.  Among other things, the copyright notice
  23. ;; and this notice must be preserved on all copies.
  24.  
  25. ;;; Commentary:
  26.  
  27. ;; When this package is loaded, `M-x mpuz' generates a random multiplication
  28. ;; puzzle.  This is a multiplication example in which each digit has been
  29. ;; consistently replaced with some letter.  Your job is to reconstruct
  30. ;; the original digits.  Type `?' while the mode is active for detailed help.
  31.  
  32. ;;; Code:
  33.  
  34. (random t)                ; randomize
  35.  
  36. (defun mpuz-random (n)
  37.   "Return a random integer between 0 and N - 1 inclusive."
  38.   (setq n (% (random) n))
  39.   (if (< n 0) (- n) n))
  40.  
  41. (defvar mpuz-silent nil
  42.   "*Set this to T if you don't want dings on inputs.")
  43.  
  44. (defun mpuz-ding ()
  45.   "Dings, unless global variable `mpuz-silent' forbids it."
  46.   (or mpuz-silent (ding t)))
  47.  
  48.  
  49. ;; Mpuz mode and keymaps
  50. ;;----------------------
  51. (defvar mpuz-mode-hook nil)
  52.  
  53. (defvar mpuz-mode-map nil
  54.   "Local keymap to use in Mult Puzzle.")
  55.  
  56. (if mpuz-mode-map nil
  57.     (setq mpuz-mode-map (make-sparse-keymap))
  58.     (define-key mpuz-mode-map "a" 'mpuz-try-letter)
  59.     (define-key mpuz-mode-map "b" 'mpuz-try-letter)
  60.     (define-key mpuz-mode-map "c" 'mpuz-try-letter)
  61.     (define-key mpuz-mode-map "d" 'mpuz-try-letter)
  62.     (define-key mpuz-mode-map "e" 'mpuz-try-letter)
  63.     (define-key mpuz-mode-map "f" 'mpuz-try-letter)
  64.     (define-key mpuz-mode-map "g" 'mpuz-try-letter)
  65.     (define-key mpuz-mode-map "h" 'mpuz-try-letter)
  66.     (define-key mpuz-mode-map "i" 'mpuz-try-letter)
  67.     (define-key mpuz-mode-map "j" 'mpuz-try-letter)
  68.     (define-key mpuz-mode-map "A" 'mpuz-try-letter)
  69.     (define-key mpuz-mode-map "B" 'mpuz-try-letter)
  70.     (define-key mpuz-mode-map "C" 'mpuz-try-letter)
  71.     (define-key mpuz-mode-map "D" 'mpuz-try-letter)
  72.     (define-key mpuz-mode-map "E" 'mpuz-try-letter)
  73.     (define-key mpuz-mode-map "F" 'mpuz-try-letter)
  74.     (define-key mpuz-mode-map "G" 'mpuz-try-letter)
  75.     (define-key mpuz-mode-map "H" 'mpuz-try-letter)
  76.     (define-key mpuz-mode-map "I" 'mpuz-try-letter)
  77.     (define-key mpuz-mode-map "J" 'mpuz-try-letter)
  78.     (define-key mpuz-mode-map "\C-g" 'mpuz-offer-abort)
  79.     (define-key mpuz-mode-map "?" 'describe-mode))
  80.  
  81. (defun mpuz-mode ()
  82.   "Multiplication puzzle mode.
  83.  
  84. You have to guess which letters stand for which digits in the
  85. multiplication displayed inside the `*Mult Puzzle*' buffer.
  86.  
  87. You may enter a guess for a letter's value by typing first the letter,
  88. then the digit.  Thus, to guess that A=3, type A 3.
  89.  
  90. To leave the game to do other editing work, just switch buffers.
  91. Then you may resume the game with M-x mpuz.
  92. You may abort a game by typing \\<mpuz-mode-map>\\[mpuz-offer-abort]."
  93.   (interactive)
  94.   (setq major-mode 'mpuz-mode
  95.     mode-name  "Mult Puzzle")
  96.   (use-local-map mpuz-mode-map)
  97.   (run-hooks 'mpuz-mode-hook))
  98.  
  99.  
  100. ;; Some variables for statistics
  101. ;;------------------------------
  102. (defvar mpuz-nb-errors 0
  103.   "Number of errors made in current game.")
  104.  
  105. (defvar mpuz-nb-completed-games 0
  106.   "Number of games completed.")
  107.  
  108. (defvar mpuz-nb-cumulated-errors 0
  109.   "Number of errors made in previous games.")
  110.  
  111.  
  112. ;; Some variables for game tracking
  113. ;;---------------------------------
  114. (defvar mpuz-in-progress nil
  115.   "True if a game is currently in progress.")
  116.  
  117. (defvar mpuz-found-digits (make-vector 10 nil)
  118.   "A vector recording which digits have been decrypted.")
  119.  
  120. (defmacro mpuz-digit-solved-p (digit)
  121.   (list 'aref 'mpuz-found-digits digit))
  122.  
  123.  
  124. ;; A puzzle uses a permutation of [0..9] into itself.
  125. ;; We use both the permutation and its inverse.
  126. ;;---------------------------------------------------
  127. (defvar mpuz-digit-to-letter (make-vector 10 0)
  128.   "A permutation from [0..9] to [0..9].")
  129.  
  130. (defvar mpuz-letter-to-digit (make-vector 10 0)
  131.   "The inverse of mpuz-digit-to-letter.")
  132.  
  133. (defmacro mpuz-to-digit (letter)
  134.   (list 'aref 'mpuz-letter-to-digit letter))
  135.  
  136. (defmacro mpuz-to-letter (digit)
  137.   (list 'aref 'mpuz-digit-to-letter digit))
  138.  
  139. (defun mpuz-build-random-perm ()
  140.   "Initialize puzzle coding with a random permutation."
  141.   (let ((letters (list 0 1 2 3 4 5 6 7 8 9)) ; new cons cells, because of delq
  142.     (index 10)
  143.     elem)
  144.     (while letters
  145.       (setq elem    (nth (mpuz-random index) letters)
  146.         letters (delq elem letters)
  147.         index   (1- index))
  148.       (aset mpuz-digit-to-letter index elem)
  149.       (aset mpuz-letter-to-digit elem index))))
  150.  
  151.  
  152. ;; A puzzle also uses a board displaying a multiplication.
  153. ;; Every digit appears in the board, crypted or not.
  154. ;;------------------------------------------------------
  155. (defvar mpuz-board (make-vector 10 nil)
  156.   "The board associates ot any digit the list of squares where it appears.")
  157.  
  158. (defun mpuz-put-digit-on-board (number square)
  159.   "Put (last digit of) NUMBER on SQUARE of the puzzle board."
  160.   ;; i.e. push SQUARE on NUMBER square-list
  161.   (setq number (% number 10))
  162.   (aset mpuz-board number (cons square (aref mpuz-board number))))
  163.  
  164. (defun mpuz-check-all-solved ()
  165.   "Check whether all digits have been solved. Return t if yes."
  166.   (catch 'found
  167.     (let ((digit -1))
  168.       (while (> 10 (setq digit (1+ digit)))
  169.     (if (and (not (mpuz-digit-solved-p digit)) ; unsolved
  170.          (aref mpuz-board digit)) ; and appearing in the puzzle !
  171.         (throw 'found nil))))
  172.     t))
  173.  
  174.  
  175. ;; To build a puzzle, we take two random numbers and multiply them.
  176. ;; We also take a random permutation for encryption.
  177. ;; The random numbers are only use to see which digit appears in which square
  178. ;; of the board. Everything is stored in individual squares.
  179. ;;---------------------------------------------------------------------------
  180. (defun mpuz-random-puzzle ()
  181.   "Draw random values to be multiplied in a puzzle."
  182.   (mpuz-build-random-perm)
  183.   (fillarray mpuz-board nil)        ; erase the board
  184.   (let (A B C D E)
  185.     ;; A,B,C,D & E, are the five rows of our multiplication.
  186.     ;; Choose random values, discarding uninteresting cases.
  187.     (while (progn
  188.          (setq A (mpuz-random 1000)
  189.            B (mpuz-random 100)
  190.            C (* A (% B 10))
  191.            D (* A (/ B 10))
  192.            E (* A B))
  193.          (or (< C 1000) (< D 1000)))) ; forbid leading zeros in C or D
  194.     ;; Individual digits are now put on their respectives squares.
  195.     ;; [NB: A square is a pair <row,column> of the screen.]
  196.     (mpuz-put-digit-on-board A         '(2 . 9))
  197.     (mpuz-put-digit-on-board (/ A 10)     '(2 . 7))
  198.     (mpuz-put-digit-on-board (/ A 100)     '(2 . 5))
  199.     (mpuz-put-digit-on-board B         '(4 . 9))
  200.     (mpuz-put-digit-on-board (/ B 10)     '(4 . 7))
  201.     (mpuz-put-digit-on-board C         '(6 . 9))
  202.     (mpuz-put-digit-on-board (/ C 10)     '(6 . 7))
  203.     (mpuz-put-digit-on-board (/ C 100)     '(6 . 5))
  204.     (mpuz-put-digit-on-board (/ C 1000)     '(6 . 3))
  205.     (mpuz-put-digit-on-board D         '(8 . 7))
  206.     (mpuz-put-digit-on-board (/ D 10)     '(8 . 5))
  207.     (mpuz-put-digit-on-board (/ D 100)     '(8 . 3))
  208.     (mpuz-put-digit-on-board (/ D 1000)     '(8 . 1))
  209.     (mpuz-put-digit-on-board E         '(10 . 9))
  210.     (mpuz-put-digit-on-board (/ E 10)     '(10 . 7))
  211.     (mpuz-put-digit-on-board (/ E 100)     '(10 . 5))
  212.     (mpuz-put-digit-on-board (/ E 1000)     '(10 . 3))
  213.     (mpuz-put-digit-on-board (/ E 10000) '(10 . 1))))
  214.  
  215. ;; Display
  216. ;;--------
  217. (defconst mpuz-framework
  218.   "
  219.      . . .
  220.                    Number of errors (this game): 0
  221.     x  . .
  222.    -------
  223.    . . . .
  224.                         Number of completed games: 0
  225.  . . . .
  226.  ---------              Average number of errors: 0.00
  227.  . . . . ."
  228.   "The general picture of the puzzle screen, as a string.")
  229.  
  230. (defun mpuz-create-buffer ()
  231.   "Create (or recreate) the puzzle buffer. Return it."
  232.   (let ((buff (get-buffer-create "*Mult Puzzle*")))
  233.     (save-excursion
  234.       (set-buffer buff)
  235.       (let ((buffer-read-only nil))
  236.     (erase-buffer)
  237.     (insert mpuz-framework)
  238.     (mpuz-paint-board)
  239.     (mpuz-paint-errors)
  240.     (mpuz-paint-statistics)))
  241.     buff))
  242.  
  243. (defun mpuz-paint-errors ()
  244.   "Paint error count on the puzzle screen."
  245.   (mpuz-switch-to-window)
  246.   (let ((buffer-read-only nil))
  247.     (goto-line 3)
  248.     (move-to-column 49)
  249.     (mpuz-delete-line)
  250.     (insert (prin1-to-string mpuz-nb-errors))))
  251.  
  252. (defun mpuz-paint-statistics ()
  253.   "Paint statistics about previous games on the puzzle screen."
  254.   (let* ((mean (if (zerop mpuz-nb-completed-games) 0
  255.            (/ (+ mpuz-nb-completed-games (* 200 mpuz-nb-cumulated-errors))
  256.               (* 2 mpuz-nb-completed-games))))
  257.      (frac-part (% mean 100)))
  258.     (let ((buffer-read-only nil))
  259.       (goto-line 7)
  260.       (move-to-column 51)
  261.       (mpuz-delete-line)
  262.       (insert (prin1-to-string mpuz-nb-completed-games))
  263.       (goto-line 9)
  264.       (move-to-column 50)
  265.       (mpuz-delete-line)
  266.       (insert (format "%d.%d%d" (/ mean 100) (/ frac-part 10) (% frac-part 10))))))
  267.  
  268. (defun mpuz-paint-board ()
  269.   "Paint board situation on the puzzle screen."
  270.   (mpuz-switch-to-window)
  271.   (let ((letter -1))
  272.     (while (> 10 (setq letter (1+ letter)))
  273.       (mpuz-paint-digit (mpuz-to-digit letter))))
  274.   (goto-char (point-min)))
  275.  
  276. (defun mpuz-paint-digit (digit)
  277.   "Paint all occurrences of DIGIT on the puzzle board."
  278.   ;; (mpuz-switch-to-window)
  279.   (let ((char (if (mpuz-digit-solved-p digit)
  280.           (+ digit ?0)
  281.           (+ (mpuz-to-letter digit) ?A)))
  282.     (square-l (aref mpuz-board digit)))
  283.     (let ((buffer-read-only nil))
  284.       (while square-l
  285.     (goto-line (car (car square-l)))    ; line before column !
  286.     (move-to-column (cdr (car square-l)))
  287.     (insert char)
  288.     (delete-char 1)
  289.     (backward-char 1)
  290.     (setq square-l (cdr square-l))))))
  291.  
  292. (defun mpuz-delete-line ()
  293.   "Clear from point to next newline."    ; & put nothing in the kill ring
  294.   (while (not (= ?\n (char-after (point))))
  295.     (delete-char 1)))
  296.  
  297. (defun mpuz-get-buffer ()
  298.   "Get the puzzle buffer if it exists."
  299.   (get-buffer "*Mult Puzzle*"))
  300.  
  301. (defun mpuz-switch-to-window ()
  302.   "Find or create the Mult-Puzzle buffer, and display it."
  303.   (let ((buff (mpuz-get-buffer)))
  304.     (or buff (setq buff (mpuz-create-buffer)))
  305.     (switch-to-buffer buff)
  306.     (or buffer-read-only (toggle-read-only))
  307.     (mpuz-mode)))
  308.  
  309.  
  310. ;; Game control
  311. ;;-------------
  312. (defun mpuz-abort-game ()
  313.   "Abort any puzzle in progress."
  314.   (message "Mult Puzzle aborted.")
  315.   (setq mpuz-in-progress nil
  316.     mpuz-nb-errors 0)
  317.   (fillarray mpuz-board nil)
  318.   (let ((buff (mpuz-get-buffer)))
  319.     (if buff (kill-buffer buff))))
  320.  
  321. (defun mpuz-start-new-game ()
  322.   "Start a new puzzle."
  323.   (message "Here we go...")
  324.   (setq mpuz-nb-errors 0
  325.     mpuz-in-progress t)
  326.   (fillarray mpuz-found-digits nil)    ; initialize mpuz-found-digits
  327.   (mpuz-random-puzzle)
  328.   (mpuz-switch-to-window)
  329.   (mpuz-paint-board)
  330.   (mpuz-paint-errors)
  331.   (mpuz-ask-for-try))
  332.  
  333. (defun mpuz-offer-new-game ()
  334.   "Ask if user wants to start a new puzzle."
  335.   (if (y-or-n-p "Start a new game ")
  336.       (mpuz-start-new-game)
  337.       (message "OK. I won't.")))
  338.  
  339. ;;;###autoload
  340. (defun mpuz ()
  341.   "Multiplication puzzle with GNU Emacs."
  342.   ;; Main entry point
  343.   (interactive)
  344.   (mpuz-switch-to-window)
  345.   (if mpuz-in-progress
  346.       (mpuz-offer-abort)
  347.       (mpuz-start-new-game)))
  348.  
  349. (defun mpuz-offer-abort ()
  350.   "Ask if user wants to abort current puzzle."
  351.   (interactive)
  352.   (if (y-or-n-p "Abort game ")
  353.       (mpuz-abort-game)
  354.       (mpuz-ask-for-try)))
  355.  
  356. (defun mpuz-ask-for-try ()
  357.   "Ask for user proposal in puzzle."
  358.   (message "Your try ?"))
  359.  
  360. (defun mpuz-try-letter ()
  361.   "Propose a digit for a letter in puzzle."
  362.   (interactive)
  363.   (if mpuz-in-progress
  364.       (let (letter-char digit digit-char message)
  365.     (setq letter-char (upcase last-command-char)
  366.           digit (mpuz-to-digit (- letter-char ?A)))
  367.     (cond ((mpuz-digit-solved-p digit)
  368.            (message "%c already solved." letter-char))
  369.           ((null (aref mpuz-board digit))
  370.            (message "%c does not appear." letter-char))
  371.           ((progn (message "%c = " letter-char)
  372.               ;; <char> has been entered.
  373.               ;; Print "<char> =" and
  374.               ;; read <num> or = <num>
  375.               (setq digit-char (read-char))
  376.               (if (eq digit-char ?=)
  377.               (setq digit-char (read-char)))
  378.               (message "%c = %c" letter-char digit-char)
  379.               (or (> digit-char ?9) (< digit-char ?0))) ; bad input
  380.            (ding t))
  381.           (t
  382.            (mpuz-try-proposal letter-char digit-char))))
  383.       (mpuz-offer-new-game)))
  384.  
  385. (defun mpuz-try-proposal (letter-char digit-char)
  386.   "Propose LETTER-CHAR as code for DIGIT-CHAR."
  387.   (let* ((letter (- letter-char ?A))
  388.      (digit (- digit-char ?0))
  389.      (correct-digit (mpuz-to-digit letter)))
  390.     (cond ((mpuz-digit-solved-p correct-digit)
  391.        (message "%c has already been found."))
  392.       ((= digit correct-digit)
  393.        (message "%c = %c correct !" letter-char digit-char)
  394.        (mpuz-ding)
  395.        (mpuz-correct-guess digit))
  396.       (t ;;; incorrect guess
  397.        (message "%c = %c incorrect !" letter-char digit-char)
  398.        (mpuz-ding)
  399.        (setq mpuz-nb-errors (1+ mpuz-nb-errors))
  400.        (mpuz-paint-errors)))))
  401.  
  402. (defun mpuz-correct-guess (digit)
  403.   "Handle correct guessing of DIGIT."
  404.   (aset mpuz-found-digits digit t)    ; Mark digit as solved
  405.   (mpuz-paint-digit digit)        ; Repaint it (now as a digit)
  406.   (if (mpuz-check-all-solved)
  407.       (mpuz-close-game)))
  408.  
  409. (defun mpuz-close-game ()
  410.   "Housecleaning when puzzle has been solved."
  411.   (setq mpuz-in-progress nil
  412.     mpuz-nb-cumulated-errors (+ mpuz-nb-cumulated-errors mpuz-nb-errors)
  413.     mpuz-nb-completed-games (1+ mpuz-nb-completed-games))
  414.   (mpuz-paint-statistics)
  415.   (let ((message (mpuz-congratulate)))
  416.     (message message)
  417.     (sit-for 4)
  418.     (if (y-or-n-p (concat message "  Start a new game "))
  419.     (mpuz-start-new-game)
  420.     (message "Good Bye !"))))
  421.  
  422. (defun mpuz-congratulate ()
  423.   "Build a congratulation message when puzzle is solved."
  424.   (format "Puzzle solved with %d errors. %s"
  425.        mpuz-nb-errors
  426.        (cond ((= mpuz-nb-errors 0)          "That's perfect !")
  427.          ((= mpuz-nb-errors 1)          "That's very good !")
  428.          ((= mpuz-nb-errors 2)          "That's good.")
  429.          ((= mpuz-nb-errors 3)          "That's not bad.")
  430.          ((= mpuz-nb-errors 4)          "That's not too bad...")
  431.          ((and (>= mpuz-nb-errors 5)
  432.                (< mpuz-nb-errors 10)) "That's bad !")
  433.          ((and (>= mpuz-nb-errors 10)
  434.                (< mpuz-nb-errors 15)) "That's awful.")
  435.          ((>= mpuz-nb-errors 15)      "That's not serious."))))
  436.  
  437. (defun mpuz-show-solution ()
  438.   "Display solution for debugging purposes."
  439.   (interactive)
  440.   (mpuz-switch-to-window)
  441.   (let (digit list)
  442.     (setq digit -1)
  443.     (while (> 10 (setq digit (1+ digit)))
  444.       (or (mpuz-digit-solved-p digit)
  445.       (setq list (cons digit list))))
  446.     (mapcar 'mpuz-correct-guess list)))
  447.  
  448. ;;; mpuz.el ends here
  449.